package com.mrjy.activity.controller;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.activity.bo.IActivityInfoBo;
import com.mrjy.activity.dto.ActivityInputDTO;
import com.mrjy.activity.dto.ActivitySelectDTO;
import com.mrjy.common.entity.ActivityInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.enums.ResultEnum;
import com.mrjy.common.util.GlobalConstant;


@Controller
@RequestMapping("/activity")
public class ActivityInfoController {
	@Resource
	private IActivityInfoBo activityBo;
	
	private static Logger Logger = LoggerFactory.getLogger(ActivityInfoController.class);
	
	/**
	 * 添加活动后台
	 * 
	 * @param ActivityInputDTO
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(@Valid ActivityInputDTO activityInputDTO,
			BindingResult bindingResult,HttpSession session) {
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null == loginUser ){
//			throw new InnerException(ResultEnum.NOT_LOGIN);
			map.put("err_code",ResultEnum.NOT_LOGIN.getCode());
			map.put("err_msg",ResultEnum.NOT_LOGIN.getMessage());
		}
		if (bindingResult.hasErrors()) {
			Logger.error("【新增活动】参数不正确, activityInputDTO={}", activityInputDTO);
	        /*throw new InnerException(ResultEnum.PARAM_ERROR.getCode(),
	                bindingResult.getFieldError().getDefaultMessage());*/
			map.put("err_code",ResultEnum.PARAM_ERROR.getCode() );
			map.put("err_msg", bindingResult.getFieldError().getDefaultMessage());
	    }
		//属性拷贝
		ActivityInfo activityInfo = new ActivityInfo();
		BeanUtils.copyProperties(activityInputDTO, activityInfo);
		return activityBo.txInsert(activityInfo);
	}
	
	/**
	 * 编辑活动后台
	 * @param ActivityInputDTO
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid ActivityInputDTO activityInputDTO,
			BindingResult bindingResult,HttpSession session) {	
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null == loginUser ){
//			throw new InnerException(ResultEnum.NOT_LOGIN);
			map.put("err_code",ResultEnum.NOT_LOGIN);
		}
		if (bindingResult.hasErrors()) {
			Logger.error("【更新活动】参数不正确, activityInputDTO={}", activityInputDTO);
	        /*throw new InnerException(ResultEnum.PARAM_ERROR.getCode(),
	                bindingResult.getFieldError().getDefaultMessage());*/
			map.put("err_code",ResultEnum.PARAM_ERROR.getCode() );
			map.put("err_msg", bindingResult.getFieldError().getDefaultMessage());
			return map;
	    }
		//属性拷贝
		ActivityInfo activityInfo = new ActivityInfo();
		BeanUtils.copyProperties(activityInputDTO, activityInfo);
		return activityBo.txUpdate(activityInfo);
	}
	
	/**
	 * 启用/禁用
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.GET)
	@ResponseBody
	public Object update(Integer activityId, Integer disableFlag,HttpSession session) {	
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null == loginUser ){
			map.put("err_msg", ResultEnum.NOT_LOGIN);
			return map;
		}
		if (null == activityId || null == disableFlag) {
			map.put("err_msg", ResultEnum.PARAM_ERROR);
			return map;
		}
		ActivityInfo activityInfo = new ActivityInfo();
		activityInfo.setId(activityId);
		activityInfo.setDisableFlag(disableFlag);
		return activityBo.txUpdate(activityInfo);
	}
	
	/**
	 * 分页查询活动列表 后台
	 * @return
	 */
	@RequestMapping(value="/back/list",method=RequestMethod.GET)
	@ResponseBody
	public Object list(ActivitySelectDTO selectDTO,HttpSession session) {
		/*UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			throw new InnerException(ResultEnum.NOT_LOGIN);
		}*/
		return activityBo.queryPageBack(selectDTO);
	}
	/**
	 * 导出
	 * @param ids
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(ActivitySelectDTO selectDTO,HttpSession session,HttpServletResponse response) {		
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			/*throw new InnerException(ResultEnum.NOT_LOGIN);*/
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("err_msg", ResultEnum.NOT_LOGIN);
			return map;
		}
		return activityBo.export(selectDTO,response);
	}
	
	
	/**
	 * 家园 查询活动列表
	 * @param userId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(HttpSession session,
			@RequestParam Integer page, 
			@RequestParam Integer rows) {		
		return activityBo.queryPage(session,page,rows);
	}
	
	/**
	 * 根据活动id查询活动详情
	 * @param session
	 * @param activityId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	@ResponseBody
	public Object detail(HttpSession session, @RequestParam(required=true)Integer activityId) {		
		return activityBo.queryById(session,activityId);
	}
	
	/**
	 * 推荐 查询直播活动列表
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/live/list")
	@ResponseBody
	public Object list(
			@RequestParam Integer page, 
			@RequestParam Integer rows) {		
		return activityBo.queryLivePage(page,rows);
	}
	/**
	 * 根据ids批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteByIds")
	@ResponseBody
	public Object deleteByIds(int[] ids) {		
		return activityBo.txDeleteByIds(ids);
	}
	
	/**
	 * 禁用、起用
	 * @param ids
	 * @return
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(ActivityInfo info,HttpSession session) {	
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null == loginUser ){
			map.put("err_msg", ResultEnum.NOT_LOGIN);
			return map;
		}
		if (null == info.getIds()) {
			map.put("err_msg", ResultEnum.PARAM_ERROR);
			return map;
		}
		return activityBo.txUpdateDisableFlag(info);
    }
	

}
